/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import init.Config;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import util.SpeakkerPlaylistUtil;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Artist.findAll", query = "SELECT a FROM Artist a ORDER BY a.artistName ASC"),
    @NamedQuery(name = "Artist.getByArtistName", query = "SELECT a FROM Artist a WHERE a.artistName=:artistName")})
public class Artist implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String avatar;
    private String artistName;
    private ArtistType type; // Solo, group, etc ...
    private Collection<Genre> genres;
    private int beginningYear;
    @OneToMany
    private Collection<Comment> comments;
    @OneToMany
    private Collection<Music> musics;

    //
    // Constructors
    //
    public Artist() {
    }

    public Artist(String artistName, ArtistType type, int beginningYear) {
        this.avatar = Config.DEFAULT_ARTIST_AVATAR_FILE;
        this.artistName = artistName;
        this.type = type;
        this.genres = new ArrayList<Genre>();
        this.beginningYear = beginningYear;
        
        this.musics =  new ArrayList<Music>();
        this.comments = new ArrayList<Comment>();
    }

    //
    // Accessors
    //
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getArtistName() {
        return artistName;
    }

    public void setArtistName(String artistName) {
        this.artistName = artistName;
    }

    public ArtistType getType() {
        return type;
    }

    public void setType(ArtistType type) {
        this.type = type;
    }

    public Collection<Genre> getGenres() {
        return genres;
    }

    public void setGenres(Collection<Genre> genres) {
        this.genres = genres;
    }

    public int getBeginningYear() {
        return beginningYear;
    }

    public void setBeginningYear(int beginningYear) {
        this.beginningYear = beginningYear;
    }

    public Collection<Comment> getComments() {
        return comments;
    }

    public void setComments(Collection<Comment> comments) {
        this.comments = comments;
    }

    public Collection<Music> getMusics() {
        return musics;
    }

    public void setMusics(Collection<Music> musics) {
        this.musics = musics;
    }
    
    
    
    // Add a genre if doesn't exists
    public void addGenre(Genre genre) {
        if(!genres.contains(genre)) {
            genres.add(genre);
        }
    }
    
    // Add a music if doesn't exists
    public void addMusic(Music music) {
        if(!musics.contains(music)) {
            musics.add(music);
        }
    }

    // Get the playlist under JSon format supported by the javascript Speakker player
    public String getJSon() {
        return SpeakkerPlaylistUtil.getJson(musics);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Artist)) {
            return false;
        }
        Artist other = (Artist) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Artist[ id=" + id + " ]";
    }
}
